﻿using System;
using System.Collections.Generic;

namespace TaDMS.Models
{
    public class UnitOfWork : IDisposable
    {
        private readonly TaDMS _context = new TaDMS();

        /*Dynamic Repository Object*/
        private Repository<Task> _taskRepository;
        private Repository<Employee> _employeeRepository;
        private Repository<TaskEmployee> _taskEmployeeRepository;
        private Repository<Report> _reportRepository;
        private Repository<Account> _accountRepository;
        private Repository<Role> _roleRepository;
        private Repository<Device> _deviceRepository;
        private Repository<Distributor> _distributorRepository;
        private Repository<Project> _projectRepository;

        /*Static Repository Object*/
        private DepartmentRepository _department;
        private JobTitleRepository _jobTitle;
        private TaskRepository _task;
        private DeviceCategoryRepository _deviceCategory;
        private DeviceRepository _device;
        private AccountRepository _account;
        private TaskEmployeeRepository _taskEmployee;
        private ReportRepository _report;

        /*Dynamic*/
        public Repository<Task> TaskRepository
        {
            get
            {
                if (this._taskRepository == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    this._taskRepository = new Repository<Task>(_context);
                }

                return _taskRepository;
            }
        }

        public Repository<Employee> EmployeeRepository
        {
            get
            {
                if (this._employeeRepository == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    this._employeeRepository = new Repository<Employee>(_context);
                }

                return _employeeRepository;
            }
        }

        public Repository<TaskEmployee> TaskEmployeeRepository
        {
            get
            {
                if (this._taskEmployeeRepository == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    _context.Configuration.LazyLoadingEnabled = false;
                    this._taskEmployeeRepository = new Repository<TaskEmployee>(_context);
                }

                return _taskEmployeeRepository;
            }
        }

        public Repository<Report> ReportRepository
        {
            get
            {
                if (this._reportRepository == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    this._reportRepository = new Repository<Report>(_context);
                }

                return _reportRepository;
            }
        }

        public Repository<Account> AccountRepository
        {
            get
            {
                if (this._accountRepository == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    this._accountRepository = new Repository<Account>(_context);
                }

                return _accountRepository;
            }
        }

        public Repository<Role> RoleRepository
        {
            get
            {
                if (this._roleRepository == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    this._roleRepository = new Repository<Role>(_context);
                }

                return _roleRepository;
            }
        }

        public Repository<Device> DeviceRepository
        {
            get
            {
                if (this._deviceRepository == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    this._deviceRepository = new Repository<Device>(_context);
                }

                return _deviceRepository;
            }
        }

        public Repository<Distributor> DistributorRepository
        {
            get
            {
                if (this._distributorRepository == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    this._distributorRepository = new Repository<Distributor>(_context);
                }

                return _distributorRepository;
            }
        }

        public Repository<Project> ProjectRepository
        {
            get
            {
                if (this._projectRepository == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    this._projectRepository = new Repository<Project>(_context);
                }

                return _projectRepository;
            }
        }

        /*Static*/
        public DepartmentRepository Department
        {
            get
            {
                if (this._department == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    _department = new DepartmentRepository(_context);
                }

                return _department;
            }
        }

        public JobTitleRepository JobTitle
        {
            get
            {
                if (this._jobTitle == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    _jobTitle = new JobTitleRepository(_context);
                }

                return _jobTitle;
            }
        }

        public TaskRepository Task
        {
            get
            {
                if (this._task == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    _task = new TaskRepository(_context);
                }

                return _task;
            }
        }

        public DeviceCategoryRepository DeviceCategory
        {
            get
            {
                if (this._deviceCategory == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    _deviceCategory = new DeviceCategoryRepository(_context);
                }

                return _deviceCategory;
            }
        }

        public DeviceRepository Device
        {
            get
            {
                if (this._device == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    //_context.Configuration.LazyLoadingEnabled = false;
                    _device = new DeviceRepository(_context);
                }

                return _device;
            }
        }

        public AccountRepository Account
        {
            get
            {
                if (this._account == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    //_context.Configuration.LazyLoadingEnabled = false;
                    _account = new AccountRepository(_context);
                }

                return _account;
            }
        }

        public TaskEmployeeRepository TaskEmployee
        {
            get
            {
                if (this._taskEmployee == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    //_context.Configuration.LazyLoadingEnabled = false;
                    _taskEmployee = new TaskEmployeeRepository(_context);
                }

                return _taskEmployee;
            }
        }

        public ReportRepository Report
        {
            get
            {
                if (this._report == null)
                {
                    _context.Configuration.ProxyCreationEnabled = false;
                    //_context.Configuration.LazyLoadingEnabled = false;
                    _report = new ReportRepository(_context);
                }

                return _report;
            }
        }

#region Dispose

        private bool _disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    this._context.Dispose();
                }
            }

            this._disposed = true;
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

#endregion

#region Unit Test
        //Private
        private RepositoryUT<Task> _taskRepositoryUT;

        //Geter
        public RepositoryUT<Task> TaskRepositoryUT
        {
            get
            {
                var testList = new List<Task>(2)
                        {
                            new Task()
                                {
                                    ActualEndDate = DateTime.Now,
                                    CreateBy = 1,
                                    CreateDate = DateTime.Now,
                                    Descriptions = "Descriptions 1",
                                    Employee = null,
                                    EndDate = DateTime.Now,
                                    StartDate = DateTime.Now,
                                    Status = "Status 1",
                                    TaskEmployees = null,
                                    TaskId = 1,
                                    TaskName = "TaskName 1",
                                    TaskType = null
                                },
                            new Task()
                                {
                                    ActualEndDate = DateTime.Now,
                                    CreateBy = 2,
                                    CreateDate = DateTime.Now,
                                    Descriptions = "Descriptions 2",
                                    Employee = null,
                                    EndDate = DateTime.Now,
                                    StartDate = DateTime.Now,
                                    Status = "Status 2",
                                    TaskEmployees = null,
                                    TaskId = 2,
                                    TaskName = "TaskName 2",
                                    TaskType = null
                                }
                        };

                if (_taskRepositoryUT == null)
                {
                    _taskRepositoryUT = new RepositoryUT<Task>(testList);
                }

                return _taskRepositoryUT;
            }
        }

#endregion

    }
}